package d1_reflect;

import org.junit.Test;

import java.lang.reflect.Field;

public class t2member {
    @Test
    public void testgetfile() throws Exception {
        Class c = Student.class;
        //获取全部成员变量
        Field[] fields = c.getDeclaredFields();
        for (Field f : fields) {
            System.out.println("名字:"+f.getName()+"类型:"+f.getType());
        }
        //定位某个成员变量
        Field field1 = c.getDeclaredField("name");
        System.out.println(field1.getName()+"--->"+ field1.getType());

        Field field2 = c.getDeclaredField("age");
        System.out.println(field2.getName()+"--->"+ field2.getType());

        Field field3 = c.getDeclaredField("height");
        System.out.println(field3.getName()+"--->"+ field3.getType());

        //赋值
        Student people =new Student();
        field1.setAccessible(true);
        field1.set(people,"多少人");
        System.out.println(people);

        //取值
        String name=(String) field1.get(people);
        System.out.println(name);
    }
}
